package com.rocpenghua.minio.controller;

import com.rocpenghua.minio.utils.MinioUtils;
import io.minio.Result;
import io.minio.messages.DeleteError;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * @author RocPengHua
 * @date 2022/6/18 17:40
 */
@RestController
@Slf4j
public class MinioController {
    @Autowired
    private MinioUtils minioUtils;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;

    @PostMapping("/upload")
    public Object upload(MultipartFile file) {
        String upload = minioUtils.uploadFile(file);
        return address+"/"+bucketName+"/"+upload;
    }
    @DeleteMapping("/remove/{fileName}")
    public Object remove(@PathVariable("fileName") String fileName) {
        boolean b = minioUtils.removeFile(fileName);
        return b?"删除成功":"删除失败";
    }
    @GetMapping("/download/{fileName}")
    public Object down(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName) {
        InputStream inputStream = minioUtils.downloadFile(fileName, response);
        return inputStream != null ? "下载成功": "下载失败";

    }
}
